﻿using Max.PH5_2103A.Shopping.TEntityFrame.DAL;
using Max.PH5_2103A.Shopping.TEntityFrame.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.PH5_2103A.Shopping.TEntityFrame.Repository
{
    /// <summary>
    /// CRUD的基本操作仓储实现
    /// </summary>
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        #region  注入

        /// <summary>
        /// 上下文对象
        /// </summary>
        MyDbContext _context;

        public BaseRepository(MyDbContext context)
        {
            _context = context;
        }
        #endregion

        #region Delete - 删除操作

       /// <summary>
       /// 删除一条数据
       /// </summary>
       /// <param name="entity"></param>
       /// <exception cref="NotImplementedException"></exception>
        public void Delete(T entity)
        {
            _context.Set<T>().Remove(entity);
        }

        /// <summary>
        /// 删除多条数据
        /// </summary>
        /// <param name="entities"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void Delete(List<T> entities)
        {
            _context.Set<T>().RemoveRange(entities);
        }

        /// <summary>
        /// 根据ID删除
        /// </summary>
        /// <param name="id"></param>
        public void Delete(object id)
        {
            //先要根据id把数据找出来
            var t = _context.Set<T>().Find(id);
            //再进行删除
            if(t!= null)
            {
                //_context.Set<T>().Remove(t);
                this.Delete(t);
            }
        }

        /// <summary>
        /// 根据ID批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void Delete(List<object> ids)
        {
            List<T> list = new List<T>();
            //先根据id循环找出对象
            ids.ForEach(x =>
            {
                var t = _context.Set<T>().Find(x);
                if(t!= null)
                {
                    //把找出来的对象放到一个集合中，然后一次性调用删除
                    list.Add(t);
                }
            });

            //批量删除
            this.Delete(list);
            
        }
        #endregion

        #region Insert - 添加操作

        /// <summary>
        /// 添加一条数据
        /// </summary>
        /// <param name="entity"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void Insert(T entity)
        {
            _context.Set<T>().Add(entity);
        }

        /// <summary>
        /// 批量添加数据
        /// </summary>
        /// <param name="entities"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void Insert(List<T> entities)
        {
            _context.Set<T>().AddRange(entities);            
        }
        #endregion

        #region Select - 查询操作

        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IQueryable<T> Select()
        {
            return _context.Set<T>();
        }

        /// <summary>
        /// 根据ID查询一条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public T SelectFirstById(object id)
        {
            return _context.Set<T>().Find(id);
        }
        #endregion

        #region Update - 修改操作

        /// <summary>
        /// 修改一条数据
        /// </summary>
        /// <param name="entityFrame"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void Update(T entityFrame)
        {
            _context.Set<T>().Update(entityFrame);
        }

        /// <summary>
        /// 修改多条数据
        /// </summary>
        /// <param name="entityFrameList"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void Update(List<T> entityFrameList)
        {
            _context.Set<T>().UpdateRange(entityFrameList);
        }
        #endregion
    }
}
